//
//  UpdateBusinessDateVC.swift
//  SBSafetyApp
//
//  Created by Aimeow on 2022/11/24.
// 修改营业日期

import Foundation
import UIKit

class UpdateBusinessDateVC: SubLevelViewController {
    var curMonth = Date().string(format: "MM")
    var curYear = Date().string(format: "yyyy")

    init(withDeptId _deptId: Int64? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.deptId = _deptId
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  
    @objc func updateButtonClicked() {
        let workDates = calendarView.calendarView.selectedDates
        var workDatesStrArray: [String] = []
        for date in workDates {
            workDatesStrArray.append(Date.normalDateFormatter2().string(from: date))
        }
        
        let year = workDates[0].string(format: "yyyy")
        let month = workDates[0].string(format: "MM")
        let end = Calendar.current.component(.day, from: Date.endOfMonth(date: workDates[0]))
        
        var workDatas: [String] = []
        var statuss: [String] = []
        
        let first = (month == curMonth && year == curYear) ? Int(Date().string(format: "dd"))! : 1
        
        for item in first...end {
            let day = "\(year)-\(month)-\(item < 10 ? "0\(item)" : "\(item)")"
            workDatas.append(day)
            if workDatesStrArray.contains(day) {
                statuss.append("1")
            } else {
                statuss.append("0")
            }
        }
        
        let workDatasStr = workDatas.joined(separator: ",")
        let statussStr = statuss.joined(separator: ",")
        
        view.showToastActivity()
        API.xgyyzt(withParam: XgyyztParam(deptId: self.deptId ?? BSUser.currentUser.deptId, month: "\(month)", remark: textViewPanel.textView.text, status: statussStr, type: "month", workDates: workDatasStr, year: nil)) { responseModel in
//            if responseModel.errorCode == .Success {
//                self.view.hideToastActivity()
//                self.navigationController?.popViewController(animated: true)
//            }
            if responseModel.errorCode == .Success {
                Utils.delay(second: 5) {
                    DispatchQueue.main.async {
                        self.view.hideToastActivity()
                        self.navigationController?.popViewController(animated: true)
                    }
                }
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "月营业日"
        setupUI()
    }
    
    func setupUI() {
        self.view.backgroundColor = UIColor.hex("#F8F8F8")
        self.view.addSubview(scrollView)
        scrollView.addSubview(contentView)
        contentView.addSubview(calendarView)
        contentView.addSubview(textViewPanel)
        contentView.addSubview(updateButton)
        
        scrollView.snp.makeConstraints { make in
            make.left.right.equalTo(view)
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view)
        }
        
        contentView.snp.makeConstraints { make in
            make.edges.equalTo(scrollView)
            make.width.equalTo(ScreenWidth)
        }
  
        calendarView.snp.makeConstraints { make in
            make.left.top.right.equalTo(contentView)
        }
        
        let remindV = TaskCRRemindView()
        remindV.configYYZT()
        contentView.addSubview(remindV)
        remindV.snp.makeConstraints { make in
            make.top.equalTo(calendarView.snp.bottom).offset(-20)
            make.centerX.equalToSuperview()
            make.width.equalTo(150)
        }
        
        textViewPanel.snp.makeConstraints { make in
            make.left.equalTo(contentView).offset(10)
            make.right.equalTo(contentView).offset(-10)
            make.top.equalTo(calendarView.snp.bottom).offset(20)
        }
        
        updateButton.snp.makeConstraints { make in
            make.left.equalTo(contentView).offset(10)
            make.right.equalTo(contentView).offset(-10)
            make.top.equalTo(textViewPanel.snp.bottom).offset(30)
            make.height.equalTo(50)
            make.bottom.equalTo(contentView.snp.bottom).offset(-30)
        }
    }
    
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        return scrollView
    }()
    
    lazy var contentView: UIView = {
        let view = UIView()
        return view
    }()
    
    lazy var calendarView: BSCalendarView = {
        let view = BSCalendarView()
        view.multipleSelect = true
        view.style = .MonthStatus
        return view
    }()
    
    lazy var textViewPanel: BaseTextViewPanel = {
        let view = BaseTextViewPanel(withStyle: .Style2, withTitle: "修改说明")
        
        return view
    }()
    
    lazy var updateButton: UIButton = {
        let button = UIButton.createCornerButton("保存修改")
        button.backgroundColor = .hex("#0F499E")
        button.addTarget(self, action: #selector(updateButtonClicked), for: .touchUpInside)
        return button
    }()
}
